﻿using System.Collections.Generic;
using LBoL.Base;
using LBoL.ConfigData;
using LBoL.Core;
using LBoL.Core.Battle;
using LBoL.Core.Battle.BattleActions;
using LBoL.Core.Battle.Interactions;
using LBoL.EntityLib.Cards.Character.Cirno;
using LBoL.EntityLib.StatusEffects.Others;
using LBoLEntitySideloader.Attributes;
using ProbeMod.Cards.Template;
using LBoL.Core.Cards;
using System.Linq;
using LBoL.Core.Units;
using LBoL.Base.Extensions;
using System;

namespace ProbeMod.Cards
{
    /// <summary>
    /// 光子炮塔
    /// </summary>
    public sealed class ProbeBCDef : ProbeCardTemplate
    {
        public override CardConfig MakeConfig()
        {
            CardConfig config = GetCardDefaultConfig();

            config.Rarity = Rarity.Common;
            config.Type = CardType.Friend;
            config.TargetType = TargetType.Nobody;
            config.Colors = new List<ManaColor>() { ManaColor.Blue };

            config.Cost = new ManaGroup() { Any = 1, Blue = 1 };
            config.UpgradedCost = new ManaGroup() { Any = 1, Blue = 1 };

            config.Loyalty = 4;
            config.UpgradedLoyalty = 4;

            //  被动耐久变化
            config.PassiveCost = -1;
            config.UpgradedPassiveCost = -1;

            //  大招消耗耐久.
            config.UltimateCost = -6;
            config.UpgradedUltimateCost = -6;

            config.Damage = 12; 
            config.UpgradedDamage = 18; 



            config.Value1 = 4;     //  大招伤害（增加值）
            config.Value2 = 6;     //  强化大招伤害（增加值）


            config.RelativeEffects = new List<string>() { "ProbeBuildingKeywordSe" };
            config.UpgradedRelativeEffects = new List<string>() { "ProbeBuildingKeywordSe" };

            config.Index = CardIndexGenerator.GetUniqueIndex(config);
            return config;
        }
    }

    [EntityLogic(typeof(ProbeBCDef))]
    public sealed class ProbeBC : ProbeFriendCard
    {
        /// <summary>
        /// 消灭敌人后被动变化值
        /// </summary>
        public int dealtLCost { get => 2; }

        public DamageInfo UltimateDamage
        {
            get
            {
                var value = IsUpgraded ? Damage.Damage + Value2 : Damage.Damage + Value1;
                return DamageInfo.Attack(value, IsAccuracy);
            }
        }

        public float UltimateDamageValue => UltimateDamage.Damage;

        protected override void OnEnterBattle(BattleController battle)
        {
            ReactBattleEvent(base.Battle.EnemyDied, OnEnemyDied);
        }

        /// <summary>
        /// 被动：-1 对生命最低目标打
        /// 如果消灭目标则不消耗耐久
        /// </summary>
        /// <returns></returns>
        public override IEnumerable<BattleAction> GetPassiveActions()
        {
            //Triigger the effect only if the card has been summoned. 
            if (!base.Summoned || base.Battle.BattleShouldEnd)
            {
                yield break;
            }
            base.NotifyActivating();
            //Increase base loyalty.
            if (base.Loyalty > 0)
            {
                base.Loyalty += base.PassiveCost;
                for (int i = 0; i < base.Battle.FriendPassiveTimes; i++)
                {
                    if (base.Battle.BattleShouldEnd)
                    {
                        break;
                    }
                    EnemyUnit target = base.Battle.EnemyGroup.Alives.MinBy((EnemyUnit unit) => unit.Hp);
                    yield return new DamageAction(base.Battle.Player, target, Damage, "Simple1");

                }
            }
            yield break;
        }

        /// <summary>
        /// 终结 -6 aoe16/22,将一张没部署的光子炮台置入手牌
        /// </summary>
        /// <param name="selector"></param>
        /// <param name="consumingMana"></param>
        /// <param name="precondition"></param>
        /// <returns></returns>
        protected override IEnumerable<BattleAction> Actions(UnitSelector selector, ManaGroup consumingMana, Interaction precondition)
        {
            base.Loyalty += base.UltimateCost;
            base.UltimateUsed = true;

            yield return new DamageAction(Battle.Player, Battle.EnemyGroup.Alives,UltimateDamage);
            var card = Library.CreateCard<ProbeBC>(IsUpgraded);
            card.Summon();
            yield return new AddCardsToHandAction(card);
        }

        private IEnumerable<BattleAction> OnEnemyDied(DieEventArgs args)
        {
            if(args.DieSource != this)
                yield break;
            Loyalty += dealtLCost;
        }
    }
}
